package com.fmyd.qizhuazhua.utils;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.fmyd.qizhuazhua.MyApplication;

import java.util.List;

/**
 * 文 件 名: ApkUtil.java
 * 描    述: APK工具类
 * 创 建 人: dingyu
 * 创建时间: 2016-1-14
 */
public class ApkUtil {
    /**
     * 根据包名获取APK版本名称
     */
    public static String getApkVersionName(Context context, String packageName) {
        try {
            if (TextUtils.isEmpty(packageName)) {
                return null;
            }
            PackageManager pm = context.getPackageManager();
            PackageInfo info = null;
            info = pm.getPackageInfo(packageName, 0);
            if (null != info) {
                return info.versionName;
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据包名获取APK版本号
     */
    public static int getApkVersionCode(Context context, String packageName) {
        try {
            if (TextUtils.isEmpty(packageName)) {
                return 0;
            }
            PackageManager pm = context.getPackageManager();
            PackageInfo info = null;
            info = pm.getPackageInfo(packageName, 0);
            if (null != info) {
                return info.versionCode;
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据文件路径获取APK包名
     */
    public static String getUnInstallApkPackageName(Context context, String apkPath) {
        try {
            if (TextUtils.isEmpty(apkPath)) {
                return null;
            }
            PackageManager pm = context.getPackageManager();
            PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
            if (null != info) {
                ApplicationInfo appInfo = info.applicationInfo;
                if (null != appInfo) {
                    return appInfo.packageName;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据文件路径获取安装APK版本号
     */
    public static int getInstallApkVersionCode(Context context, String apkPath) {
        try {
            if (TextUtils.isEmpty(apkPath)) {
                return 0;
            }
            PackageManager pm = context.getPackageManager();
            PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
            if (null != info) {
                return info.versionCode;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据文件路径获取安装APK版本名称
     */
    public static String getInstallApkVersionName(Context context, String apkPath) {
        try {
            if (TextUtils.isEmpty(apkPath)) {
                return null;
            }
            PackageManager pm = context.getPackageManager();
            PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
            if (null != info) {
                return info.versionName;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 安装APK
     */
    public static void installApk(Context context, String apkPath) {
        try {
            if (TextUtils.isEmpty(apkPath)) {
                return;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.setDataAndType(Uri.fromFile(new File(apkPath)), "application/vnd.android.package-archive");
            intent.setDataAndType(Uri.parse("file://" + apkPath), "application/vnd.android.package-archive");
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 启动APK应用
     */
    public static synchronized void open(String packageName) {
        try {
            if (TextUtils.isEmpty(packageName)) {
                return;
            }
            Context context = MyApplication.instance;
            Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 检查APK是否安装
     */
    public static boolean checkApkInstalled(Context context, String packageName) {
        try {
            if (TextUtils.isEmpty(packageName)) {
                return false;
            }
            context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);//hezong 2017-03-13
//            context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_INSTRUMENTATION);
            return true;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }
    /*
    Must be one or more of: 
    PackageManager.GET,META,DATA, 
    PackageManager.GET,SHARED,LIBRARY,FILES, 
    android.content.pm.PackageManager.MATCH,UNINSTALLED,PACKAGES, 
    android.content.pm.PackageManager.MATCH,SYSTEM,ONLY, 
    android.content.pm.PackageManager.MATCH,DISABLED,UNTIL,USED,COMPONENTS, 
    PackageManager.GET,DISABLED,UNTIL,USED,COMPONENTS, 
    PackageManager.GET,UNINSTALLED,PACKAGES
    
     */

    /**
     * 检查应用是否在运行
     */
    public static boolean isTopActivity(Context context, String packageName) {
        try {
            if (TextUtils.isEmpty(packageName)) {
                return false;
            }
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
            if (tasksInfo.size() > 0) {
                // 应用程序位于堆栈的顶层
                if (packageName.equals(tasksInfo.get(0).topActivity.getPackageName())) {
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取AndroidManifest.xml 里的meta-data值
     */
    public static String getMetaDataValue(Context context, String key) {
        Bundle metaData = null;
        String value = null;
        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
                    context.getPackageName(), PackageManager.GET_META_DATA);
            if (null != ai)
                metaData = ai.metaData;
            if (null != metaData) {
                value = metaData.getString(key);

            }
        } catch (NameNotFoundException e) {

        }
        return value;
    }


}
